<script setup>
import { ref, onMounted } from 'vue'
import { queryAllApi, addApi, queryByIdApi, updateApi, deleteApi } from '@/api/dept'
import { ElMessage, ElMessageBox } from 'element-plus'

onMounted(() => {
  search();
})
const search = async () => {
  const result = await queryAllApi();
  if (result.code) {
    deptList.value = result.data;
  }
}

//表单
const dialogFormVisible = ref(false)
const dept = ref({ name: '' })
const formTitle = ref('')
const deptFormRef = ref()
const save = async () => {
  //表单校验
  if (!deptFormRef.value) return;
  await deptFormRef.value.validate(async (valid) => {
    if (valid) {
      let result;
      if (dept.value.id) { // 修改
        result = await updateApi(dept.value);
      } else { // 新增
        result = await addApi(dept.value);
      }
      if (result.code) {
        ElMessage.success('新增成功');
        dialogFormVisible.value = false;
        search();
      } else {
        ElMessage.error('新增失败');
      }
    } else {
      ElMessage.error('表单校验不通过');
    }
  })
}

const addDept = () => {
  dialogFormVisible.value = true;
  formTitle.value = '新增部门';
  dept.value = {};
  //重置表单提示信息
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
}

const deptList = ref([])


//表单校验
const rules = ref({
  name: [
    { required: true, message: '部门名称是必填项', trigger: 'blur' },
    { min: 2, max: 10, message: '部门名称的长度应该为2-10位', trigger: 'blur' },
  ]
})

//编辑
const edit = async (id) => {
  formTitle.value = '修改部门';
  //重置表单提示信息
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
  const result = await queryByIdApi(id);
  if (result.code) {
    dialogFormVisible.value = true;
    dept.value = result.data;
  }
}

//删除
const deleteById = async (id) => {
  ElMessageBox.confirm(
    '此操作将永久删除该部门, 是否继续?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(async () => {
    const result = await deleteApi(id);
    if (result.code) {
      ElMessage.success('删除成功');
      search();
    } else {
      ElMessage.error(result.msg);
    }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除',
    })
  })
}
</script>

<template>
  <h1>部门管理</h1>
  <div class="container" @click="addDept">
    <el-button type="primary">+ 新增部门</el-button>
  </div>
  <div class="container">
    <el-table :data="deptList" border style="width: 100%">
      <el-table-column type="index" label="序号" width="100" align="center" />
      <el-table-column prop="name" label="部门名称" width="260" align="center" />
      <el-table-column prop="updateTime" label="最后操作时间" width="300" align="center" />
      <el-table-column prop="address" label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" @click="edit(scope.row.id)"><el-icon>
              <EditPen />
            </el-icon>编辑</el-button>
          <el-button type="danger" size="small" @click="deleteById(scope.row.id)"><el-icon>
              <Delete />
            </el-icon>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!-- 新增部门 -->
  <el-dialog v-model="dialogFormVisible" :title="formTitle" width="500">
    <el-form :model="dept" :rules="rules" ref="deptFormRef">
      <el-form-item label="部门名称" label-width="80px" prop="name">
        <el-input v-model="dept.name" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.container {
  margin: 10px 0px;
}
</style>
